//给定两个二叉树，想象当你将它们中的一个覆盖到另一个上时，两个二叉树的一些节点便会重叠。 
//
// 你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠，那么将他们的值相加作为节点合并后的新值，否则不为 NULL 的节点将直接作为新二叉树的节点
//。 
//
// 示例 1: 
//
// 
//输入: 
//	Tree 1                     Tree 2                  
//          1                         2                             
//         / \                       / \                            
//        3   2                     1   3                        
//       /                           \   \                      
//      5                             4   7                  
//输出: 
//合并后的树:
//	     3
//	    / \
//	   4   5
//	  / \   \ 
//	 5   4   7
// 
//
// 注意: 合并必须从两个树的根节点开始。 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 
// 👍 800 👎 0

package com.cute.leetcode.editor.cn;
public class MergeTwoBinaryTrees {
    public static void main(String[] args) {
        TreeNode root1 = new TreeNode(1);
        TreeNode t1Node1 = new TreeNode(3);
        TreeNode t1Node2 = new TreeNode(2);
        TreeNode t1Node3 = new TreeNode(5);
        root1.left = t1Node1;root1.right = t1Node2;t1Node1.left = t1Node3;

        TreeNode root2 = new TreeNode(2);
        TreeNode t2Node1 = new TreeNode(1);
        TreeNode t2Node2 = new TreeNode(3);
        TreeNode t2Node3 = new TreeNode(4);
        TreeNode t2Node4 = new TreeNode(7);
        root2.left = t2Node1;root2.right = t2Node2;t2Node1.right = t2Node3; t2Node2.right = t2Node4;
        new MergeTwoBinaryTrees().new Solution().mergeTrees(root1, root2);
    }
    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        /**
         * 这样写倒是也没有问题，是按照我的思路直接写的，会有一些多余的判断
         * 要是两个节点只要有一个是null就返回的话会简单许多，就是题解中的方法
         */
        /*if (root1 == null && root2 == null) return null;
        if (root1 == null){
            root1 = new TreeNode(root2.val);
        }else if (root2!=null){
            root1.val += root2.val;
        }else {
            root2 = new TreeNode(-1);
        }
        TreeNode tempL = mergeTrees(root1.left, root2.left);
        TreeNode tempR = mergeTrees(root1.right, root2.right);
        if (root1.left == null) root1.left = tempL;
        if (root1.right == null) root1.right = tempR;
        return root1;*/

        /**
         * 题解中的解法：
         * 1.传入的两个节点如果有一个为null直接返回不为空的节点
         * 2.全不为null时进行相加
         * 3.前序遍历，递归修改root1的节点，一定要接收节点才行
         */
        if (root1 == null || root2 == null) return root1 == null? root2:root1;
        root1.val += root2.val;
        root1.left = mergeTrees(root1.left, root2.left);
        root1.right = mergeTrees(root1.right, root2.right);
        return root1;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}